import 'package:equatable/equatable.dart';

/// 帖子分类枚举
enum PostCategory {
  plantGrowthChallenge('Plant Growth Challenge', 'assets/images/category1.png'),
  herbRevival30Day(
    'Herb Revival 30-Day Challenge',
    'assets/images/category2.png',
  ),
  upcycledPlanterGreen(
    'Upcycled Planter Green Challenge',
    'assets/images/category3.png',
  );

  const PostCategory(this.displayName, this.imagePath);
  final String displayName;
  final String imagePath;
}

/// 资源类型枚举
enum ResourceType {
  image('Image'),
  video('Video');

  const ResourceType(this.displayName);
  final String displayName;
}

/// 植物养殖帖子模型
/// 使用Equatable实现相等性比较
class PostModel extends Equatable {
  /// 帖子ID
  final String id;

  /// 发布用户ID
  final String userId;

  /// 帖子分类
  final PostCategory category;

  /// 帖子标题
  final String title;

  /// 帖子详情文字
  final String content;

  /// 标签列表（最多5个）
  final List<String> tags;

  /// 资源类型
  final ResourceType resourceType;

  /// 资源文件路径
  final String resourcePath;

  /// 视频封面图路径（仅当resourceType为video时使用）
  final String? coverImagePath;

  /// 发布时间
  final DateTime publishTime;

  const PostModel({
    required this.id,
    required this.userId,
    required this.category,
    required this.title,
    required this.content,
    this.tags = const [],
    required this.resourceType,
    required this.resourcePath,
    this.coverImagePath,
    required this.publishTime,
  });

  /// 从JSON创建帖子对象
  factory PostModel.fromJson(Map<String, dynamic> json) {
    return PostModel(
      id: json['id'] as String,
      userId: json['userId'] as String,
      category: PostCategory.values.firstWhere(
        (e) => e.name == json['category'],
        orElse: () => PostCategory.plantGrowthChallenge,
      ),
      title: json['title'] as String,
      content: json['content'] as String,
      tags: List<String>.from(json['tags'] as List? ?? []),
      resourceType: ResourceType.values.firstWhere(
        (e) => e.name == json['resourceType'],
        orElse: () => ResourceType.image,
      ),
      resourcePath: json['resourcePath'] as String,
      coverImagePath: json['coverImagePath'] as String?,
      publishTime: DateTime.parse(json['publishTime'] as String),
    );
  }

  /// 转换为JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId': userId,
      'category': category.name,
      'title': title,
      'content': content,
      'tags': tags,
      'resourceType': resourceType.name,
      'resourcePath': resourcePath,
      'coverImagePath': coverImagePath,
      'publishTime': publishTime.toIso8601String(),
    };
  }

  /// 复制并修改部分属性
  PostModel copyWith({
    String? id,
    String? userId,
    PostCategory? category,
    String? title,
    String? content,
    List<String>? tags,
    ResourceType? resourceType,
    String? resourcePath,
    String? coverImagePath,
    DateTime? publishTime,
  }) {
    return PostModel(
      id: id ?? this.id,
      userId: userId ?? this.userId,
      category: category ?? this.category,
      title: title ?? this.title,
      content: content ?? this.content,
      tags: tags ?? this.tags,
      resourceType: resourceType ?? this.resourceType,
      resourcePath: resourcePath ?? this.resourcePath,
      coverImagePath: coverImagePath ?? this.coverImagePath,
      publishTime: publishTime ?? this.publishTime,
    );
  }

  /// 验证标签数量（最多5个）
  bool get isValidTagsCount => tags.length <= 5;

  /// 验证视频资源是否有封面图
  bool get isValidVideoResource {
    if (resourceType == ResourceType.video) {
      return coverImagePath != null && coverImagePath!.isNotEmpty;
    }
    return true;
  }

  /// 验证帖子数据是否有效
  bool get isValid {
    return title.isNotEmpty &&
        content.isNotEmpty &&
        isValidTagsCount &&
        isValidVideoResource;
  }

  @override
  List<Object?> get props => [
    id,
    userId,
    category,
    title,
    content,
    tags,
    resourceType,
    resourcePath,
    coverImagePath,
    publishTime,
  ];

  @override
  String toString() {
    return 'PostModel(id: $id, userId: $userId, category: $category, title: $title, resourceType: $resourceType)';
  }
}
